package org.vectrics.recruiter.user.resume.email;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;

public class WordExtractorEnglish implements WordExtractor {
	private transient Logger log = Logger.getLogger(this.getClass());

	public WordExtractorEnglish() {
		super();
	}
	
	public WordList getWordList(String content) {
		WordList wordList = new WordList();
		String currentWord = "";
		int i = 0;
		int contentLength = content.length();
		
		char[] chars = content.toCharArray();
		
		while (i < contentLength) {
			char c = chars[i];
			if (charIsWhiteSpace(c)) {
				if (currentWord.length() > 0) {
					Word word = new Word(currentWord, Word.TYPE_WORD);
					wordList.add(word);
					currentWord = "";
				}
				
				String whiteSpace = " ";	
				while ((i < contentLength) && (this.charIsWhiteSpace(c))) {
					//whiteSpace += "" + c;
					i++;
					if (i < contentLength)
						c = chars[i];
				}
				Word word = new Word(whiteSpace, Word.TYPE_WHITESPACE);
				wordList.add(word);
			} else {
				if ((currentWord.length() > 1) && (this.isPunctuationInWordMiddle(c))) {
					// Add the first word and the punctuation to the word list. 
					Word word = new Word(currentWord, Word.TYPE_WORD);
					wordList.add(word);
					
					word = new Word("" + c, Word.TYPE_PUNCTUATION);
					wordList.add(word);
					currentWord = "";
					i++;
				} else if (this.isPunctuationInWordMiddle(c)) {
					if (currentWord.length() > 0) {
						Word word = new Word(currentWord, Word.TYPE_WORD);
						wordList.add(word);
						currentWord = "";
					}
					Word word = new Word("" + c, Word.TYPE_PUNCTUATION);
					wordList.add(word);
					currentWord = "";
					i++;
					
				} else {
					currentWord = currentWord + c;
					i++;
				}
			}
		}

		if (currentWord.length() > 0) {
			Word word = new Word(currentWord, Word.TYPE_WORD);
			currentWord = "";
			wordList.add(word);
		}
		
		log.debug("At this point, words might have punctuation at the end.  Need to remove punctuation and make into its own word.");
		wordList = parsePunctuation(wordList);
		
		return(wordList);
	}
	
	public boolean charIsWhiteSpace(char c) {
		boolean result = false;
		if ((c == '\r') || (c == '\n') || (c == ' ') || (c == 9) 
			//	|| (c == '?')  
				|| (c == '&')  
			//|| (c == '!') 
			|| (c == ',') 
			//|| (c == ';') || (c == ':')
			|| (c == '<') || (c == '>') || (c == '"')
			 //|| (c == '\'') 
			 || (c == '`')
					|| (c == '#')|| (c == '{')|| (c == '}')
					|| (c == '[')|| (c == ']') || (c == '*')|| (c == '=')
					|| (c == '+')|| (c == '\\')|| (c == '/') || (c > 'z')) {

			result = true;
		}
		return(result);
	}

	/*
	protected WordList splitWordsWithMiddlePunctuation(WordList beforeList) {
		WordList afterList = new WordList();
		Iterator iter  = beforeList.iterator();
		while (iter.hasNext()) {
			Word word = (Word)iter.next();
			if ((word.getType().equals(Word.TYPE_WORD))
					&& (word.getValue().length() >= 3)
					&& (this.wordHasMiddlePunctuation(word)) ) {
				int 
				
			} else {
				afterList.add(word);
			}
			
		}
		return(afterList);
	}
	*/
	
	protected WordList parsePunctuation(List beforeList) {
		//List wordList = splitWordsWithMiddlePunctuation(beforeList);
		
		WordList afterList = new WordList();
		Iterator iter  = beforeList.iterator();
		while (iter.hasNext()) {
			Word word = (Word)iter.next();
			if (word.getValue().length() == 1) {
				if (isPunctuation(word.getValue().charAt(0))) {
					word.setType(Word.TYPE_PUNCTUATION);
					afterList.add(word);
				} else {
					afterList.add(word);
				}
			} else {
				log.debug("Get last character and see if it is punctuation.");
				char lastChar = word.getValue().charAt(word.getValue().length() - 1);
				if (this.isPunctuation(lastChar)) {
					String newValue = word.getValue().substring(0, word.getValue().length() - 1);
					word.setValue(newValue);
					afterList.add(word);
					
					String punctValue = "" + lastChar;
					Word punctuation = new Word(punctValue, Word.TYPE_PUNCTUATION);
					afterList.add(punctuation);
				} else {
					afterList.add(word);
				}
			}
		}
		return(afterList);
	}
	
	
	protected boolean isPunctuation(char c) {
		boolean result = false;
		if ((c == '?') 
				|| (c == '.') 
				|| (c == '!') || (c == ',') || (c == ';') 
				|| (c == ':')|| (c == '<')|| (c == '>')|| (c == '"') || (c == '\'') || (c == '`')
				|| (c == '#')|| (c == ')')|| (c == '(')|| (c == '{')|| (c == '}')
				|| (c == '[')|| (c == ']') || (c == '*')|| (c == '=')
				|| (c == '+')|| (c == '\\')) {
			result = true;
		}
		return(result);
	}

	
	protected boolean isPunctuationInWordMiddle(char c) {
		boolean result = false;
		if ((c == '?') 
				|| (c == '.')  
				|| (c == '!') || (c == ',') || (c == ';') 
				|| (c == ':')|| (c == '<')|| (c == '>')|| (c == '"') 
				//|| (c == '\'') 
				//|| (c == '`')
				|| (c == '#') || (c == ')')|| (c == '(')|| (c == '{')|| (c == '}')
				|| (c == '[') || (c == ']') || (c == '*')|| (c == '=')
				|| (c == '+') || (c == '\\')) {
			result = true;
		}
		return(result);
	}
	
	
}
